package com.magic.interview.模拟.LC2034股票价格波动;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author laoma
 * @create 2022-01-23 10:58
 */
public class StockPrice {
    int cur;
    // 维护时间戳出现的价格
    Map<Integer, Integer> map = null;
    // 维护最高最低价格
    TreeMap<Integer, Integer> tm = null;

    public StockPrice() {
        cur = Integer.MIN_VALUE;
        map = new HashMap<>();
        tm = new TreeMap<>();
    }

    public void update(int timestamp, int price) {
        cur = Math.max(cur, timestamp);
        if ( map.containsKey(timestamp) ){
            var old = map.get(timestamp);
            var cnt = tm.get(old);
            if(cnt == 1) tm.remove(old);
            else tm.put(old,cnt-1);
        }
        map.put(timestamp,price);
        tm.put(price,tm.getOrDefault(price,0) + 1);
    }

    public int current() {
        return map.get(cur);
    }

    public int maximum() {
        return tm.lastKey();
    }

    public int minimum() {
        return tm.firstKey();
    }


}
